﻿using System.Web.Mvc;
using tour.szhome.common;
using TOUR.SZHOME.Attributes;
using TOUR.SZHOME.BLL;
using TOUR.SZHOME.Entity;

namespace tour.szhome.manage.area.Controllers
{
    [SelfOnly]
    public class UnitController : Controller
    {
        //
        // GET: /Unit/
        Projects_UnitBLL _unitbll = new Projects_UnitBLL();

        /// <summary>
        /// 户型列表视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Index(int id = 0, string pname = "")
        {
            ViewBag.ID = id;
            ViewBag.PName = pname;
            return View();
        }

        /// <summary>
        /// 户型详情页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Detail(int id = 0)
        {
            ViewBag.ID = id;
            Projects_UnitEntity model = new Projects_UnitEntity();
            model = _unitbll.GetUnitByID(id);
            return View(model);
        }

        /// <summary>
        /// 户型编辑
        /// </summary>
        /// <param name="id">户型ID</param>
        /// <returns></returns>
        public ActionResult Edit(int id = 0,int pid = 0, string pname = "")
        {
            Projects_UnitEntity model = new Projects_UnitEntity();
            if (id > 0)
            {
                model = _unitbll.GetUnitByID(id);
            }
            ViewBag.ID = id;
            ViewBag.PID = pid;
            ViewBag.PName = pname;
            ViewBag.uploadAddress = System.Configuration.ConfigurationManager.AppSettings["UploadAddress"];//获取上传文件地址 
            ViewBag.Folder = "big/";   //获取当前子文件夹
            return View(model);
        }

        /// <summary>
        /// 户型编辑
        /// </summary>
        /// <returns></returns>
        [ActionName("json_edit")]
        public ActionResult Json_Edit(Projects_UnitEntity model)
        {
            return Json(_unitbll.EditUnit(model));
        }

        /// <summary>
        /// 根据项目ID获取户型列表
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [ActionName("get_unit_list")]
        public ActionResult GetUnitList(int projectId = 0, int page = 1, int size = 10)
        {
            PageSet pageSet = new PageSet()
            {
                PageIndex = page,
                PageSize = size,
            };
            var data = new
            {
                page = page,
                pageSize = size,
                list = _unitbll.GetUnitListByPID(projectId, pageSet),
                recordCount = pageSet.RecordCount,
                totalpage = pageSet.PageCount
            };
            return Json(data);
        }

        /// <summary>
        /// 根据ID删除对象
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [ActionName("json_delete")]
        public ActionResult DelByID(int uid) 
        {
            return Json(_unitbll.DelByID(uid), JsonRequestBehavior.AllowGet);
        }
    }
}
